<template>
  <!-- 添加或修改企业场所授权对话框 -->
  <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body :close-on-click-modal="false"
    @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
      size="small">
        <el-form-item label="信用代码" prop="entCode">
          <el-input v-model.trim="form.entCode" placeholder="请输入信用代码" />
      </el-form-item>

      <el-form-item label="场所代码" prop="fieldCode">
        <el-select v-model.trim="form.fieldCode" placeholder="请选择场所代码">
          <el-option v-for="dict in dict.type.entity_basefieldinfo" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="做单授权" prop="genBillsMark">
        <el-select v-model.trim="form.genBillsMark" placeholder="请选择做单授权">
          <el-option v-for="dict in dict.type.yes_or_no" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="申报授权" prop="declBillsMark">
        <el-select v-model.trim="form.declBillsMark" placeholder="请选择申报授权">
          <el-option v-for="dict in dict.type.yes_or_no" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="查询授权" prop="queryDataMark">
        <el-select v-model.trim="form.queryDataMark" placeholder="请选择查询授权">
          <el-option v-for="dict in dict.type.yes_or_no" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="一站式授权" prop="osdMark">
        <el-select v-model.trim="form.osdMark" placeholder="请选择一站式授权">
          <el-option v-for="dict in dict.type.yes_or_no" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="申请说明" prop="applyNote">
        <el-input v-model.trim="form.applyNote" placeholder="请输入申请说明" />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getEnterpriseFieldAuth,
    addEnterpriseFieldAuth,
    updateEnterpriseFieldAuth
  } from "@/api/system/enterpriseFieldAuth";
  export default {
    name: "EnterpriseFieldAuth-addEdit",
    props: {
      dialogType: {
        type: String,
        default: "add",
      },
      rowData: {
        type: Object,
        default: () => {},
      },
      dict: {}
    },
    data() {
      return {
        // 弹出层
        dialogVisible: false,
        // 标题
        title: "新增",
        // 表单参数
        form: {
          id: null,
          entCode: null,
          fieldCode: null,
          genBillsMark: null,
          declBillsMark: null,
          queryDataMark: null,
          osdMark: null,
          applyTime: null,
          applyNote: null,
          authStatus: null,
          authRemark: null,
          auther: null,
          authTime: null

        },
        // 企业场所授权表格数据
        enterpriseFieldAuthList: [],
        // 表单校验
        rules: {
          entCode: [{
            required: true,
            message: "企业代码不能为空",
            trigger: "change"
          }],
          fieldCode: [{
            required: true,
            message: "场所代码不能为空",
            trigger: "change"
          }],
          genBillsMark: [{
            required: true,
            message: "做单授权不能为空",
            trigger: "change"
          }],
          declBillsMark: [{
            required: true,
            message: "申报授权不能为空",
            trigger: "change"
          }],
          queryDataMark: [{
            required: true,
            message: "查询授权不能为空",
            trigger: "change"
          }],
          osdMark: [{
            required: true,
            message: "一站式授权不能为空",
            trigger: "change"
          }],
          applyNote: [{
            required: true,
            message: "申请说明不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    methods: {
      open() {
        this.dialogVisible = true;
        this.$nextTick(() => {
          if (this.dialogType == "add") {
            this.title = "新增";
          } else if (this.dialogType == "edit") {
            this.title = "编辑";
            this.loadDetail();
          } else if (this.dialogType == "detail") {
            this.title = "详情";
            this.loadDetail();
          }
        });
      },
      close() {
        this.$refs["form"].resetFields();
        this.dialogVisible = false;
      },

      // 获取详情
      loadDetail() {
        const id = this.rowData.id;
        getEnterpriseFieldAuth(id).then((res) => {
          if (res.code == 200) {
            this.form = res.data;
          }
        });
      },

      // 提交表单
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let params = {
              ...this.form,
            };
            if (this.dialogType == "add") {
              addEnterpriseFieldAuth(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            } else {
              params.id = this.rowData.id;
              updateEnterpriseFieldAuth(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            }
          }
        });
      },
    },
  };
</script>

<style lang="scss" scoped></style>
